<template>
  <div class="flex">
    <validator name='v'>
      <form novalidate class="form-horizontal flex">
        <p class="bg-info text-center" style="padding: 8px;">退费管理</p>

        <div class="row" style="margin-top:10px;">
          <label class = "control-label col-sm-1">姓名</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_user_name" type="text" class= "form-control" v-model = "row.f_user_name" disabled value="">
          </div>
          <label class = "control-label col-sm-1">电话</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_user_phone"    type="text" class= "form-control" v-model = "row.f_user_phone" disabled value="">
          </div>
          <label class = "control-label col-sm-1">地址</label>
          <div class="col-sm-5">
            <input id = "coo_refund_f_address"    type="text" class= "form-control" v-model = "row.f_address" disabled value="">
          </div>
        </div>
        <div class="span" style="margin-top:10px;">
          <criteria-paged :model="refundata" v-ref:paged >
          <data-grid :model="model" partial='list' v-ref:grid class = "overflow:auto">
            <template partial='head'>
              <tr>
                 <th>序号</th>
                 <th>用户编号</th>
                 <th>上期指数</th>
                 <th>本期指数</th>
                 <th>欠费日期</th>
                 <th>应交气量</th>
                 <th>应交金额</th>
                 <!-- <th>账户余额</th>
                 <th>欠费金额</th> -->
                 <th>滞纳金</th>
              </tr>
            </template>
            <template partial='body'>
              <td style="text-align: center;">{{$index+1}}</td>
              <td style="text-align: center;">{{row.f_userinfo_id}}</td>
              <td style="text-align: center;">{{row.f_last_tablebase.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.f_tablebase.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.f_input_date}}</td>
              <td style="text-align: center;">{{row.f_oughtamount.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.f_oughtfee.toFixed(2)}}</td>
              <!-- <td style="text-align: center;">{{row.f_balance.toFixed(2)}}</td>
              <td style="text-align: center;">{{row.qianfei.toFixed(2)}}</td> -->
              <td style="text-align: center;">{{row.overdue.toFixed(2)}}</td>
            </template>
          </data-grid>
          </criteria-paged>
        </div>
        <div class="row" style="margin-top:10px;">
          <label class = "control-label col-sm-2">账户余额</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_balance"    type="text" class= "form-control" v-model = "row.f_balance" disabled value="">
          </div>
          <label class = "control-label col-sm-2">欠费金额</label>
          <div class="col-sm-2">
            <input id = "coo_refund_f_qianfei"    type="text" class= "form-control" v-model= "(refundata.sums.f_oughtfee + refundata.sums.overdue) ? (refundata.sums.f_oughtfee + refundata.sums.overdue) : 0 " disabled value="">
          </div>
          <label  class = "control-label "   v-if = "(refundata.sums.f_oughtfee + refundata.sums.overdue) > row.f_balance" style = "color:red;margin-left:10px">注: 欠费金额大于账户余额，无法退费</label>
        </div>
        <div class="row" style="margin-top:10px;" >
          <label class = "control-label col-sm-2">打印格式</label>
          <div class="col-sm-2" id="ComprehenOperation-refMan-打印格式">
            <v-select
              v-model="model.printype"
              placeholder='打印格式'
              :value.sync="model.printype"
              :options='printype'
              condition=""
              close-on-select >
            </v-select>
          </div>
          <!-- <div class="col-sm-2">
            <input id = "coo_refund_"    type="text" class= "form-control" v-model = "" value="">
          </div> -->
          <label class = "control-label col-sm-2">是否销户</label>
          <div class="col-sm-2" id="ComprehenOperation-refMan-是否销户">
            <v-select
              v-model="model.userdelete"
              placeholder='是否销户'
              :value.sync="model.userdelete"
              :options='userdele'
              close-on-select >
            </v-select>
          </div>
          <!-- <div class="col-sm-2">
            <input id = "coo_refund_"    type="text" class= "form-control" v-model = "" value="">
          </div> -->
          <div v-show = "(refundata.sums.f_oughtfee + refundata.sums.overdue) < row.f_balance" class="form-group" :class="[$v.refundmoney.max || $v.refundmoney.min || $v.refundmoney.required ? 'has-error' : 'has-success']">
            <label class = "control-label col-sm-2">退费金额</label>
            <div class="col-sm-2" v-if= "model.userdelete[0] === '是' ">
              <input id = "coo_refund_refundmoney"    type="number" class= "form-control" v-model= "model.refundmoney" v-validate:refundmoney='{max: row.f_balance - (refundata.sums.f_oughtfee + refundata.sums.overdue),min: 0, required: true}' disabled  value="{{row.f_balance - (refundata.sums.f_oughtfee + refundata.sums.overdue)}}" >
            </div>
            <div class="col-sm-2" v-else>
              <input id = "coo_refund_refundmoney"    type="number" class= "form-control" v-model= "model.refundmoney" v-validate:refundmoney='{max: row.f_balance - (refundata.sums.f_oughtfee + refundata.sums.overdue),min: 0, required: true}' value="">
            </div>

          </div>
        </div>
        <div v-show = "(refundata.sums.f_oughtfee + refundata.sums.overdue) < row.f_balance" class="row" style="margin-top:10px;">
          <div class="form-group" :class="[$v.f_refundreason.required ? 'has-error' : 'has-success']">
            <label for="f_refundreason" class=" col-sm-2 control-label">退费原因:</label>
            <div class="col-sm-10">
              <textarea id = "coo_refund_refundreason" class="form-control" v-model= "model.refundreason" v-validate:f_refundreason='{required: true }' rows="3" placeholder="请填写原因"></textarea>
            </div>
          </div>
        </div>
      </form>
      <!-- <print-bill :show="print" v-ref:printbill :data='billdata' v-on:toggle="close"></print-bill> -->
    </validator>
    <div style="text-align:right;height:auto;margin-top:30px;">
      <button class="btn btn-success" @click="confirm()"  :disabled='!$v.valid || refundata.sums.f_oughtfee > row.f_balance' id="ComprehenOperation-refMan-确认">确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
      <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;" id="ComprehenOperation-refMan-取消">取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
    </div>
  </div>
</template>
<script>
/**
*综合业务
*退费管理组件
*/
import { PagedList } from 'vue-client'
import AppData from '../../../../../stores/AppData'
import * as Util from '../../../../Util'

export default {
  title: '退费管理',
  data () {
    return {
      refundata: '',
      print: false,
      billdata: {
        logicmodel: {}
      },
      model: {
        printype: '国税发票',
        userdelete: '否'
      }
    }
  },
  props: ['row'],
  ready () {
    this.getrefundata()
  },
  watch: {
    'row' () {
      // this.getrefundata()
    }
  },
  methods: {
    // 查询欠费的记录
    getrefundata () {
      this.refundata = new PagedList('rs/sql/getArreaUser', 10, {condition: `"u.f_userinfo_id = ${this.row.f_userinfo_id}"`}, {f_oughtamount: '', f_oughtfee: '', f_balance: '', overdue: ''})
      this.refundata.search()
    },
    confirm () {
      // 整理数据 发送请求
      let data = this.sortdata()
      this.$resetpost('rs/logic/refundManage', data, {resolveMsg: '退费成功', rejectMsg: '退费失败'})
      .then((res) => {
        let backdata = res.data
        this.billdata = Object.assign(this.billdata, this.row)
        this.billdata.id = backdata.id
        this.billdata.f_bill_type = '用户退费'
        this.billdata.f_bill_style = this.model.printype
        this.billdata.logicmodel.f_preamount = this.model.refundmoney
        this.billdata.logicmodel.f_collection = this.model.refundmoney
        this.billdata.logicmodel.f_print = this.model.printype
        // this.print = true
        // this.$refs.printbill.bill.search(backdata.id)
        this.$dispatch('success', '用户退费', this.row, res)
      }).catch((error) => {
        if (error.status === 603) {
          this.$error('重复提交')
          return
        }
        // this.$dispatch('error', '用户退费', this.row, error)
      })
    },
    clean () {
      this.$dispatch('clean', '退费管理', this.row)
    },
    sortdata () {
      // 用户表余额 用户状态 更改
      let user = {
        f_userinfo_id: this.row.f_userinfo_id,
        f_balance: this.row.f_balance - this.model.refundmoney,
        version: this.row.userinfo_version
      }

      // 如果有销户操作，产生销户数据
      let cancel
      if (this.model.userdelete[0] === '是') {
        // 用户数据中 更改用户状态为销户
        user.f_user_state = '销户'
        // 产生的销户表数据 和 记录表数据
        cancel = {
          userinfo: this.row.f_userinfo_id,
          f_user_id: this.row.f_user_id,
          f_user_name: this.row.f_user_name,
          f_cancel_state: '有效',
          f_cancel_cause: '用户退费, 同时销户',
          f_operator: Util.f.name,
          // 同时产生 记录数据
          f_operat_type: '销户',
          f_describe: `对用户${this.row.f_user_name}进行退费, 同时进行销户操作`,
          f_state: '有效',
          f_filiale: Util.f.f_fengongsi,
          f_outlets: Util.f.f_parentname
        }
      }

      // sellinggas 收费表数据
      let sellgas = {
        f_balance: this.row.f_balance,
        f_curbalance: this.row.f_balance - this.model.refundmoney,
        f_tablebase: 0,
        f_last_tablebase: 0,
        f_meter_type: this.row.f_meter_classify,
        f_payment: '现金缴费',
        f_price_id: this.row.f_price_id,
        f_user_id: this.row.f_user_id,
        f_user_name: this.row.f_user_name,
        f_user_type: this.row.f_user_type,
        f_gasproperties: this.row.f_gasproperties,
        f_address: this.row.f_address,
        userinfo: this.row.f_userinfo_id,
        f_meter_brand: this.row.f_meter_brand,
        f_meter_style: this.row.f_meter_style,
        f_meternumber: this.row.f_meternumber,
        f_card_id: this.row.f_card_id,
        f_collection: (-this.model.refundmoney),
        f_operator: Util.f.name,
        f_filiale: Util.f.f_fengongsi,
        f_outlets: Util.f.f_parentname,
        f_charge_type: '用户退费',
        f_charge_state: '有效',
        f_comments: this.model.refundreason,
        // 记录表数据
        f_operat_type: '用户退费',
        f_describe: `对用户${this.row.f_user_name}进行退费, 退费金额为${this.model.refundmoney}。`,
        f_state: '有效'
      }
      let data = {user: user, sellgas: sellgas, model: this.model, cancel: cancel}
      return data
    }
  },
  computed: {
    printype () {
      return [...AppData.getParam('打印格式')]
    },
    userdele () {
      return [...AppData.getParam('是否')]
    }
  }
}
</script>

<style>
</style>
